<?php

namespace app\merchants\controller;

use app\common\validate\merchant\MerchantOrderPointValid;
use app\common\validate\merchant\MerchantProductPointClassValid;
use app\common\validate\merchant\MerchantProductPointValid;
use app\merchants\loader\Services;
use think\Exception;

/**
 * 积分商城
 */
class Point extends ApiBase
{
    /**
     * 商品列表 分页
     */
    public function product_page()
    {
        try {
            $data = $this->request->get();
            $options = $this->formatOption($data);
            //需要获取商户id
            $where[] = ['mer_id', '=', $this->merId];
            //删除时间
            $where[] = ['delete_at', '=', null];

            if (!empty($data['keyword'])) {
                $where[] = ['title|code', 'like', '%' . $data['keyword'] . '%'];
            }

            if (!empty($data['class_id'])) {
                $where[] = ['class_id', '=', $data['class_id']];
            }
            $options['where'] = $where;
            $res = Services::Point()->getPage($options);

            api_result($res);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 商品列表
     * @return  void
     * @throws \Exception
     */
    public function product_list(): void
    {
        try {
            $param = $this->request->get();
            $where[] = ['mer_id','=', $this->merId];
            if (!empty($data['keyword'])) {
                $where[] = ['title', 'like','%'.trim($param['keyword']).'%'];
            }
            $where[] = ['delete_at','=',null];
            $data = Services::Point()->getList($where , $param['limit'] ?: 10);
            api_result(API_SUCCESS, '请求成功', $data);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 商品保存
     * @return  void
     * @throws \Exception
     */
    public function product_save(): void
    {
        try {
            $param = $this->request->post();
            $scene = '.add';
            if (!empty($param['id'])) {
                $scene = '.edit';
            }
            //数据拦截验证
            $this->validate($param, MerchantProductPointValid::class . $scene);
            $array = array_merge($param, ['op_user' => $this->adminUser['admin_id'], 'mer_id' => $this->merId]);

            Services::Point()->save($array);
            api_result(API_SUCCESS, '保存成功');
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 商品删除
     * @return  void
     * @throws \Exception
     */
    public function product_del(): void
    {
        try {
            $param = $this->request->post();
            $this->validate($param, MerchantProductPointValid::class . '.del');
            Services::Point()->del($param['id'], $this->merId);
            api_result(API_SUCCESS, '请求成功', ['id' => $param['id']]);
        } catch (Exception $e) {
            api_result($e);
        }


    }

    /**
     * 商品详情
     * @return  void
     * @throws \Exception
     */
    public function product_info(): void
    {
        try {
            $param = $this->request->get();
            $this->validate($param, MerchantProductPointValid::class . '.info');
            $data = Services::Point()->info(['id' => $param['id'], 'mer_id' => $this->merId]);
            api_result(API_SUCCESS, '请求成功', $data);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 商品修改任意字段
     * @return  void
     * @throws \Exception
     */
    public function product_save_field(): void
    {
        try {
            $param = $this->request->post();
            $this->validate($param, MerchantProductPointValid::class . '.info');
            Services::Point()->updateField(['id' => $param['id'], 'mer_id' => $this->merId], $param);
            api_result(API_SUCCESS, '请求成功', $param['id']);
        } catch (Exception $e) {
            api_result($e);
        }
    }

#分类===================================================================================

    /**
     * 分类分页列表
     */
    public function class_list(): void
    {
        try {
            //需要获取商户id
            $data = input('get.');
            $where[] = ['mer_id', '=', $this->adminUser['mer_id']];
            if (!empty($data['keyword'])) {
                $where[] = ['title', '=', $data['keyword']];
            }
            $limit = (int)(empty($data['limit']) ? 10 : $data['limit']);
            $data = Services::MerchantProductPointClass()->getList($where, $limit);
            api_result($data);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 分类保存
     * @throws \Exception
     */
    public function class_save(): void
    {
        try {
            $data = input('post.');
            validate(MerchantProductPointClassValid::class)->scene('class_save')->check($data);
            $array = array_merge($data, ['op_user' => $this->adminUser['admin_id'], 'mer_id' => $this->adminUser['mer_id']]);
            $id = (int)(empty($data['id']) ? 0 : $data['id']);
            $id = Services::MerchantProductPointClass()->save($array, $id);
            api_result(API_SUCCESS, '请求成功', ['id' => $id]);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 分类删除
     */
    public function class_del(): void
    {
        try {
            $data = input('post.');
            validate(MerchantProductPointClassValid::class)->scene('class_del')->check($data);
            Services::MerchantProductPointClass()->del($data['id'], $this->adminUser['mer_id']);
            api_result(API_SUCCESS, '请求成功',['id' => $data['id']]);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 分类详情
     * @return  void
     */
    public function class_info(): void
    {
        try {
            $data = input('get.');
            validate(MerchantProductPointClassValid::class)->scene('class_info')->check($data);
            $data = Services::MerchantProductPointClass()->info($data['id']);
            api_result(API_SUCCESS, '请求成功', $data);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 分类修改排序
     * @return  void
     */
    public function class_save_sort(): void
    {
        try {
            $data = input('post.');
            validate(MerchantProductPointClassValid::class)->scene('class_save_sort')->check($data);
            Services::MerchantProductPointClass()->save_sort($data['id'], $data['sort']);
            api_result(API_SUCCESS, '请求成功', ['id' => $data['id']]);
        } catch (Exception $e) {
            api_result($e);
        }
    }


#订单============================================================================
    /**
     * 积分订单 会员
     */
    public function member_order_page()
    {
        $this->order_page(USER_TYPE_MEMBER);
    }

    /**
     * 积分订单
     */
    protected function order_page($type = USER_TYPE_SALESMAN)
    {
        try {
            $data = $this->request->get();
            $options = $this->formatOption($data);
            //需要获取商户id
            $where[] = ['mer_id', '=', $this->merId];
            $where[] = ['user_type', '=', $type];

            if (!empty($data['keyword'])) {
                $where[] = ['mobile|order_no|product_title|product_code', 'like', '%' . trim($data['keyword']) . '%'];
            }
            //状态
            if (isset($data['status']) && $data['status'] !== 'all') {
                $where[] = ['status', '=', $data['status']];
            }
            //根据时间搜索
            if (!empty($data['stime']) && empty($data['etime'])) {
                $where[] = ['create_at', '>=', $data['stime']];
            }
            if (empty($data['stime']) && !empty($data['etime'])) {
                $where[] = ['create_at', '<=', $data['etime']];
            }
            if (!empty($data['stime']) && !empty($data['etime'])) {
                $where[] = ['create_at', 'between time', [$data['stime'], $data['etime']]];
            }
            $options['where'] = $where;
            $list = Services::Point()->getOrderPage($options, $type);
            api_result(API_SUCCESS, '请求成功', $list);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 积分订单 导购
     */
    public function shopper_order_page()
    {
        $this->order_page();
    }


    private  function order_send($user_type){
        $data = input('post.');
        try {
            $this->validate($data,MerchantOrderPointValid::class.'.order_send');
            Services::Point()->OrderSend(
                $data['order_no'],
                $data['express_company'],
                $data['express_no'],
                $this->adminUser['admin_id'],
                $this->merId,
                $user_type,
            );
            api_result(API_SUCCESS, '请求成功', ['order_on' => $data['order_no']]);
        }catch (Exception $e){
            api_result($e);
        }
    }

    /**
     * 导购积分订单发货
     */
    public  function shopper_order_send(){
        $this->order_send(USER_TYPE_SALESMAN);
    }

    /**
     * 会员积分订单发货
     */
    public  function member_order_send(){
        $this->order_send(USER_TYPE_MEMBER);
    }
}
